package leetcode;

import java.util.ArrayList;

public class Minesweeper {

	private final int[][] dir = {{1, 0} , {-1, 0}, {0, 1}, {0, -1}, 
			{-1, -1}, {-1, 1}, {1, -1}, {1 , 1} };
	
    public char[][] updateBoard(char[][] board, int[] click) {
        if(board == null || board.length <= 0 || click == null || click.length < 2){
        	return board;
        }
        int m = board.length;
        int n = board[0].length;
        
        int x = click[0];
        int y = click[1];
        //如果点中了雷
        if(board[x][y] == 'M'){
        	board[x][y] = 'X';
        	return board;
        }
        board[x][y] = 'B';
        //如果点中的是空白
        int count = 0;
        int[] index = new int[8];
        for(int i = 0; i < dir.length; i++){
        	int curX = x + dir[i][0];
        	int curY = y + dir[i][1];
        	if(curX < 0 || curX >= m || curY < 0 || curY >= n){
        		continue;
        	}
        	if(board[curX][curY] == 'E'){
        		index[i] = (i + 1);
        	}else if(board[curX][curY] == 'M'){
        		count++;
        	}
        }
        if(count != 0){
        	//如果当前格子周围有雷，就不应该改递归翻开周围的空格
        	board[x][y] = (char)(count + '0');
        }else{
        	for(int i = 0; i < index.length; i++){
        		if(index[i] != 0){
        			updateBoard(board, new int[]{x + dir[index[i] - 1][0], y + dir[index[i] - 1][1]});
        		}
        	}
        }
        return board;
    }
//    public void judge(int x, int y, int m, int n){
//    	int count = 0;
//        for(int i = 0; i < dir.length; i++){
//        	int curX = x + dir[i][0];
//        	int curY = y + dir[i][1];
//        	if(curX < 0 || curX >= m || curY < 0 || curY >= n){
//        		continue;
//        	}
//        	
//        }
//    }
}
